/**
 * 班组角色标准作业过程设置
 */
Ext.define('Module.jc.JcBzjsbzzy', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.jcBzjsbzzyPanel',
	layout: 'border',
	initComponent : function() {
		var me = this;
		var store=Ext.create('Ext.data.Store',{
			autoLoad: false,
			fields: ['id','zygcName','bzzyName','upuser','uptime','name','jcBzjsId','jcBzzyZygcId'],
	        proxy: {
	            type: 'ajax',
	            url: 'jc/JcBzjsbzzyAction/getBzzyZygcByIdList.do'
	        }
		});
		
		var menutree =me.menutree=  Ext.create('Ext.tree.Panel',{
			title:"班组角色",
			rootVisible: false,
			border: true,
			singleExpand: true,
			region: 'west',
			width: 300,
			id:"jcBzjs_tree_id",
			expanded: true, 
			store: Ext.create('Ext.data.TreeStore', {
		        proxy: {
		            type: 'ajax',
		            url: 'jc/JcBzjsbzzyAction/getJcBzjsbzzyTree.do'
		        },
		        root: {
		        	expanded: true,  
		            id: '0'
		        }
		    }),
		    tbar:[{  
		        xtype: 'trigger',  
		        triggerCls: 'x-form-clear-trigger',  
		        onTriggerClick: function () {  
		            this.setValue('');  
		            treeFilter(menutree,'');
		        },  
		        width:'100%',  
		        emptyText:'请输入过滤条件',  
		        enableKeyEvents: true,  
		        listeners: {  
		            keyup: {  
		                fn: function (field, e) {  
		                	var v=field.getValue();
		                	treeFilter(menutree,v);
		                }  
		            }  
		        }   
		    }],   
			listeners:{
				select:function(r,c){
					if(typeof(c.raw.pid) !='undefined' ){
						Ext.getCmp(me.id+'jcBzjsId').setValue(c.raw.pid);
						jcBzjsbzzyPanel.store.load({ 
							params: {
					    	jcBzjsId:c.raw.pid
							},
			    			callback: function(records, operation, success) {
			    				Ext.Ajax.request({
			    					url: 'jc/JcBzjsbzzyAction/getJcBzjsbzzyByIdList.do',
								    params: {
								    	jcBzjsId:c.raw.pid
								    },
								    success: function(response){
								    	var rs = Ext.decode(response.responseText);
				       	    	    	for ( var i = 0; i < rs.length; i++) {
				       	    	    		jcBzjsbzzyPanel.getSelectionModel().select(jcBzjsbzzyPanel.store.findRecord('jcBzzyZygcId',rs[i].jcBzzyZygcId),true,true);
										}
								    }
								});
			    			}
			    		});
					}
					
				}
			}
		});
		   //设置复选框
        var sm = new Ext.selection.CheckboxModel({
        	checkOnly : false,
			enableKeyNav:true
        });
		
		var jcBzjsbzzyPanel = me.jcBzjsbzzyPanel = Ext.create('Ext.grid.Panel',{
			title: '班组角色标准作业信息列表',
			region: 'center',
			padding: '0 1 0 0',
			selModel:sm,
			store: store,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
					name: 'jcBzjsId',
					id:me.id+"jcBzjsId",
					xtype: 'hidden'
				},{
						xtype: 'toolbar',
						items: [{
							text: '保存',
							iconCls: 'icon-save',
							handler: function(){
								var erec = Ext.getCmp(me.id+'jcBzjsId').getValue(); 
								if(erec !=null){
									var pries = [];
								    var recs = jcBzjsbzzyPanel.getSelectionModel().getSelection();
								    for ( var i = 0; i < recs.length; i++) {
										pries.push(recs[i].data.jcBzzyZygcId);
									}
									Ext.Ajax.request({
									    url: 'jc/JcBzjsbzzyAction/addJcBzjsbzzy.do',
									    params: {
									    	jcBzjsId: erec,
									    	jcBzzyZygcIds: pries.join(",")
									    },
									    success: function(response){
				        	    	    	var rs = Ext.decode(response.responseText);
				        	    	    	if(rs.rst){
				        	    	    		Ext.Msg.alert('提示', '保存成功！');
				        	    	    	}
				        	    	    	else{
				        	    	    		Ext.Msg.alert('提示', '保存失败！详情：'+rs.msg);
				        	    	    	}
				        	    	    }
									});
								}
								else{
									Ext.Msg.alert('提示', '请在企业列表中选择一个企业！');
								}
							}
						}/*,{
					    	text: '刷新',
					    	iconCls: 'icon-arrow_refresh',
					    	handler: function(){
					    		jcBzjsbzzyPanel.store.load();
					    	}
					    }*/
					]
					 }]
			}],		
			columns: [{
	            text: '所属过程',
	            flex: 3,
	            dataIndex: 'zygcName'
			}]
		});

		me.items = [menutree,jcBzjsbzzyPanel];

		me.callParent();
	}
});